import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import {MoviesService} from '../movies.service'
import {Movie} from '../movie'
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
  movies:Movie[];
  keyword:string;
  searchList:Movie[];
  constructor(private route: ActivatedRoute,
              private location: Location,
              private moviesService:MoviesService,
              private message: NzMessageService) { }
  searchMovies(){
    //this.movies=this.moviesService.searchMovies(keyword);
    console.log("search keyword:",this.keyword);
    this.moviesService.getSearchMovies(this.keyword).subscribe(res=>{
      console.log("搜索结果：",res);
      if(res.code==0){
        this.movies=res.data;
      }else{
        this.message.create('error',res.msg);
      }
    })
  }
  //根据输入框内容搜索电影
  getSearchList(){
    console.log("关键字：",this.keyword);
    this.moviesService.getSearchMovies(this.keyword).subscribe(res=>{
      if(res.code==0){
        this.searchList=res.data;
      }else{
        this.message.create('error',res.msg);
      }
    })
  }
  ngOnInit(): void {
    this.keyword = this.route.snapshot.paramMap.get('keyword');
    this.searchMovies();
  }

}
